<template>
  <el-dialog
    :title="!dataForm.oldId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id" prop="mapId">
      <el-input v-model="dataForm.mapId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="二手房价格" prop="oldPrice">
      <el-input v-model="dataForm.oldPrice" placeholder="二手房价格"></el-input>
    </el-form-item>
    <el-form-item label="二手房面积" prop="oldArea">
      <el-input v-model="dataForm.oldArea" placeholder="二手房面积"></el-input>
    </el-form-item>
    <el-form-item label="二手房户型(1.一室 2.二室 3.三室)" prop="oldRoom">
      <el-input v-model="dataForm.oldRoom" placeholder="二手房户型(1.一室 2.二室 3.三室)"></el-input>
    </el-form-item>
    <el-form-item label="二手房类型(1.住宅 2.别墅 3.商业 4.写字楼))" prop="oldType">
      <el-input v-model="dataForm.oldType" placeholder="二手房类型(1.住宅 2.别墅 3.商业 4.写字楼))"></el-input>
    </el-form-item>
    <el-form-item label="二手房状态" prop="oldStatus">
      <el-input v-model="dataForm.oldStatus" placeholder="二手房状态"></el-input>
    </el-form-item>
    <el-form-item label="二手房装修(1.精装修 2.普通装修 3.毛坯房)" prop="oldFinish">
      <el-input v-model="dataForm.oldFinish" placeholder="二手房装修(1.精装修 2.普通装修 3.毛坯房)"></el-input>
    </el-form-item>
    <el-form-item label="二手房特色(1.近地铁 2.环境好 3.低价)" prop="oldFeat">
      <el-input v-model="dataForm.oldFeat" placeholder="二手房特色(1.近地铁 2.环境好 3.低价)"></el-input>
    </el-form-item>
    <el-form-item label="二手房图片" prop="oldImg">
      <el-input v-model="dataForm.oldImg" placeholder="二手房图片"></el-input>
    </el-form-item>
    <el-form-item label="二手房简介" prop="oldIntro">
      <el-input v-model="dataForm.oldIntro" placeholder="二手房简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          oldId: 0,
          mapId: '',
          oldPrice: '',
          oldArea: '',
          oldRoom: '',
          oldType: '',
          oldStatus: '',
          oldFinish: '',
          oldFeat: '',
          oldImg: '',
          oldIntro: ''
        },
        dataRule: {
          mapId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          oldPrice: [
            { required: true, message: '二手房价格不能为空', trigger: 'blur' }
          ],
          oldArea: [
            { required: true, message: '二手房面积不能为空', trigger: 'blur' }
          ],
          oldRoom: [
            { required: true, message: '二手房户型(1.一室 2.二室 3.三室)不能为空', trigger: 'blur' }
          ],
          oldType: [
            { required: true, message: '二手房类型(1.住宅 2.别墅 3.商业 4.写字楼))不能为空', trigger: 'blur' }
          ],
          oldStatus: [
            { required: true, message: '二手房状态不能为空', trigger: 'blur' }
          ],
          oldFinish: [
            { required: true, message: '二手房装修(1.精装修 2.普通装修 3.毛坯房)不能为空', trigger: 'blur' }
          ],
          oldFeat: [
            { required: true, message: '二手房特色(1.近地铁 2.环境好 3.低价)不能为空', trigger: 'blur' }
          ],
          oldImg: [
            { required: true, message: '二手房图片不能为空', trigger: 'blur' }
          ],
          oldIntro: [
            { required: true, message: '二手房简介不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.oldId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.oldId) {
            this.$http({
              url: this.$http.adornUrl(`/house/oldhouse/info/${this.dataForm.oldId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapId = data.oldHouse.mapId
                this.dataForm.oldPrice = data.oldHouse.oldPrice
                this.dataForm.oldArea = data.oldHouse.oldArea
                this.dataForm.oldRoom = data.oldHouse.oldRoom
                this.dataForm.oldType = data.oldHouse.oldType
                this.dataForm.oldStatus = data.oldHouse.oldStatus
                this.dataForm.oldFinish = data.oldHouse.oldFinish
                this.dataForm.oldFeat = data.oldHouse.oldFeat
                this.dataForm.oldImg = data.oldHouse.oldImg
                this.dataForm.oldIntro = data.oldHouse.oldIntro
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/oldhouse/${!this.dataForm.oldId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'oldId': this.dataForm.oldId || undefined,
                'mapId': this.dataForm.mapId,
                'oldPrice': this.dataForm.oldPrice,
                'oldArea': this.dataForm.oldArea,
                'oldRoom': this.dataForm.oldRoom,
                'oldType': this.dataForm.oldType,
                'oldStatus': this.dataForm.oldStatus,
                'oldFinish': this.dataForm.oldFinish,
                'oldFeat': this.dataForm.oldFeat,
                'oldImg': this.dataForm.oldImg,
                'oldIntro': this.dataForm.oldIntro
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
